package com.jiutianniao.p2p.acctrans.core.facade.impl;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.jws.WebService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.jiutianniao.p2p.acctrans.core.constants.AccountTransConst;
import com.jiutianniao.p2p.acctrans.core.dal.model.AccountDO;
import com.jiutianniao.p2p.acctrans.core.service.AccountService;
import com.jiutianniao.p2p.base.model.Money;
import com.jiutianniao.p2p.trans.client.enums.AccountTypeEnum;
import com.jiutianniao.p2p.trans.client.enums.BalanceDirectionEnum;
import com.jiutianniao.p2p.trans.client.facade.AccountOpenFacade;
import com.jiutianniao.p2p.trans.client.result.AccountOpenResult;

@WebService(endpointInterface = "com.jiutianniao.p2p.trans.client.facade.AccountOpenFacade", serviceName = "AccountOpenFacade")
@Service("accountOpenFacade")
public class AccountOpenFacadeImpl implements AccountOpenFacade {

	@Resource
	private AccountService accountService;

	@Override
	public AccountOpenResult openAccountForUser(Long userId,
			Map<String, Object> extraAttribute) {
		AccountOpenResult result = new AccountOpenResult();
		Date now = new Date();

		try {

			AccountDO accountDO = new AccountDO();
			String newAccountNo = AccountTransConst.yiqihao_account_prefix
					+ "0" + AccountTypeEnum.INVESTORS_ACCOUNT.getCode()
					+ userId + "0" + AccountTransConst.RMB_CURRENCY;
			accountDO.setAccountNo(newAccountNo);
			accountDO
					.setAccountTitle(AccountTransConst.DEFAULT_ACCOUNT_TITLE_PRIVATE);
			accountDO.setAccountType(AccountTypeEnum.INVESTORS_ACCOUNT
					.getCode());
			String str = (String) extraAttribute.get("aliasName");
			accountDO.setAliasName(StringUtils.isEmpty(str) ? "" : str);
			accountDO.setBalance(new Money(0, 0));
			accountDO.setBalanceDirection(BalanceDirectionEnum.CREDIT_DIRECTION
					.getCode());
			accountDO.setCreditAmount(new Money(0, 0));
			accountDO.setCtuAmount(new Money(0, 0));
			accountDO.setCurrency(AccountTransConst.RMB_CURRENCY);
			accountDO.setDepositAmount(new Money(0, 0));
			accountDO.setEnabledStatus(null);
			accountDO.setFreezeAmount(new Money(0, 0));
			accountDO.setLastModifiedDt(now);
			accountDO
					.setLastModifiedOperator(AccountTransConst.DEFAULT_OPERATOR);
			accountDO.setRepeatPasswordErrorTimes(0);
			accountDO.setSystemAmount(new Money(0, 0));
			accountDO.setUserId(userId);
			accountService.creatNewAccount(accountDO);
			result.setAccountNo(newAccountNo);
		} catch (Exception e) {
			result.setResultCode("exception");
			return result;
		}
		result.setSuccess(true);
		return result;
	}

	@Override
	public AccountOpenResult openAccountForInternal(String title,
			Map<String, Object> extraAttribute) {
		AccountOpenResult result = new AccountOpenResult();
		Date now = new Date();

		String sequenceIdStr = accountService.getAccountNextSequenceStr();

		String newAccountNo = AccountTransConst.yiqihao_account_prefix + "0"
				+ AccountTypeEnum.INTERNAL_ACCOUNT.getCode() + sequenceIdStr
				+ "0" + AccountTransConst.RMB_CURRENCY;
		AccountDO accountDO = new AccountDO();
		accountDO.setAccountNo(newAccountNo);
		accountDO.setAccountTitle(AccountTransConst.DEFAULT_ACCOUNT_TITLE_OWN);
		accountDO.setAccountType(AccountTypeEnum.INTERNAL_ACCOUNT.getCode());
		String str = (String) extraAttribute.get("aliasName");
		accountDO.setAliasName(StringUtils.isEmpty(str) ? "" : str);
		accountDO.setBalance(new Money(0, 0));
		accountDO.setBalanceDirection(BalanceDirectionEnum.CREDIT_DIRECTION
				.getCode());
		accountDO.setCreditAmount(new Money(0, 0));
		accountDO.setCtuAmount(new Money(0, 0));
		accountDO.setCurrency(AccountTransConst.RMB_CURRENCY);
		accountDO.setDepositAmount(new Money(0, 0));
		accountDO.setEnabledStatus(null);
		accountDO.setFreezeAmount(new Money(0, 0));

		accountDO.setLastModifiedDt(now);
		accountDO.setLastModifiedOperator(AccountTransConst.DEFAULT_OPERATOR);
		accountDO.setRepeatPasswordErrorTimes(0);
		accountDO.setSystemAmount(new Money(0, 0));
		accountDO.setUserId(AccountTransConst.DEFAULT_OPERATOR);
		accountService.creatNewAccount(accountDO);

		result.setAccountNo(newAccountNo);
		result.setSuccess(true);
		return result;
	}

	@Override
	public AccountOpenResult openAccountForAsset(String title,
			Map<String, Object> extraAttribute) {
		AccountOpenResult result = new AccountOpenResult();
		Date now = new Date();
	
		AccountDO accountDO = new AccountDO();
		String sequenceIdStr = accountService.getAccountNextSequenceStr();
		String newAccountNo = AccountTransConst.yiqihao_account_prefix + "0"
				+ AccountTypeEnum.INTERNAL_ACCOUNT.getCode() + sequenceIdStr
				+ "0" + AccountTransConst.RMB_CURRENCY;
		accountDO.setAccountNo(newAccountNo);
		accountDO
				.setAccountTitle(AccountTransConst.DEFAULT_ACCOUNT_TITLE_ASSET);
		accountDO.setAccountType(AccountTypeEnum.INTERNAL_ACCOUNT.getCode());
		String str = (String) extraAttribute.get("aliasName");
		accountDO.setAliasName(StringUtils.isEmpty(str) ? "" : str);
		accountDO.setBalance(new Money(0, 0));
		accountDO.setBalanceDirection(BalanceDirectionEnum.DEBIT_DIRECTION
				.getCode());
		accountDO.setCreditAmount(new Money(0, 0));
		accountDO.setCtuAmount(new Money(0, 0));
		accountDO.setCurrency(AccountTransConst.RMB_CURRENCY);
		accountDO.setDepositAmount(new Money(0, 0));
		accountDO.setEnabledStatus(null);
		accountDO.setFreezeAmount(new Money(0, 0));

		accountDO.setLastModifiedDt(now);
		accountDO.setLastModifiedOperator(AccountTransConst.DEFAULT_OPERATOR);
		accountDO.setRepeatPasswordErrorTimes(0);
		accountDO.setSystemAmount(new Money(0, 0));
		accountDO.setUserId(AccountTransConst.DEFAULT_OPERATOR);
		accountService.creatNewAccount(accountDO);

		result.setAccountNo(newAccountNo);
		result.setSuccess(true);
		return result;
	}

}
